package ru.ifmo.contest.games;

import java.io.*;
import java.util.*;

/**
 * @author avhaliullin
 */

public class TaskC {
    FastScanner in;
    PrintWriter out;


    public void solve() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();
        boolean[] reserved = new boolean[n + 1];
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node();
        }
        boolean[] hasParent = new boolean[n];
        for (int i = 0; i < m; i++) {
            int from = in.nextInt() - 1;
            int to = in.nextInt() - 1;
            hasParent[to] = true;
            nodes[from].children.add(nodes[to]);
        }

        Queue<Node> queue = new LinkedList<Node>();
        for (int i = 0; i < n; i++) {
            if (!hasParent[i]) {
                queue.offer(nodes[i]);
            }
        }

        Deque<Node> stack = new ArrayDeque<Node>();
        while (!queue.isEmpty()) {
            stack.addLast(queue.poll());
            while (!stack.isEmpty()) {
                Node current = stack.getLast();
                if (current.ready) {
                    stack.pollLast();
                    //backward
                    if (current.children.size() == 1) {
                        current.grundy = current.children.get(0).grundy == 0 ? 1 : 0;
                        continue;
                    }
                    Arrays.fill(reserved, 0, current.children.size() + 1, false);
                    for (Node child : current.children) {
                        reserved[child.grundy] = true;
                    }
                    int i = 0;
                    while (reserved[i]) {
                        i++;
                    }
                    current.grundy = i;
                    continue;
                }
                if (current.children.isEmpty()) {
                    stack.pollLast();
                    current.ready = true;
                    continue;
                }
                //forward
                for (Node child : current.children) {
                    stack.addLast(child);
                }
                current.ready = true;
            }
        }
        for (Node node : nodes) {
            out.println(node.grundy);
        }

    }

    public void run(String fileName) {
        try {
            in = new FastScanner(new File(fileName + ".in"));
            out = new PrintWriter(new File(fileName + ".out"));

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class Node {
        List<Node> children = new ArrayList<Node>();
        int grundy = 0;
        boolean ready = false;
    }


    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] arg) {
        new TaskC().run("grundy");
    }

}
